package com.goldgov.materials.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.gtiles.core.web.json.ClientMessage;
import org.gtiles.core.web.json.ClientMessage.severity_level;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.materials.service.Materials;
import com.goldgov.materials.service.MaterialsQuery;
import com.goldgov.materials.service.MaterialsService;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/workbench/materials")
@Api("学习资料服务接口")
public class MaterialsController {
	
	@Autowired
	private MaterialsService service;
	
	@GetMapping("/preAddMaterials")
	@ApiOperation(value = "预新增学习资料", notes ="查询学习资料ID")
	public JsonSuccessObject<Materials> preAddMaterials(@ApiIgnore Materials obj,HttpServletRequest request){
		obj = service.getMaterialsID();
		JsonSuccessObject<Materials> returnResult = new JsonSuccessObject<>(obj);
		ClientMessage m=new ClientMessage();
		m.setMessage_severity(severity_level.success.toString());
		m.setMessage_text("执行成功");
		ClientMessage[] array= {m};
		returnResult.setClientmessagearray(array);
		return returnResult;
	}
	
	@PostMapping
	@ApiOperation(value = "新增学习资料", notes ="")
	public JsonObject<Object> addMaterials(Materials obj,HttpServletRequest request){
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		obj.setCreateUserID(u.getEntityID());
		obj.setCreateUserName(u.getUserName());
		obj.setPublishState(MaterialsQuery.PUBLISH_STATE_NO);
		obj.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		if(null==obj.getMaterialsCoverID()||obj.getMaterialsCoverID()=="") {
			obj.setMaterialsCoverID(MaterialsQuery.MATERIALS_DEFAULT);
		}
		service.addMaterials(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@DeleteMapping
	@ApiOperation(value = "批量学习资料删除", notes ="删除学习资料")
	public JsonObject<Object> deleteMaterials(@ApiParam(value = "materialsIDs", required = true) @RequestParam("ids") String[] ids){
		service.deleteMaterials(ids);
		return JsonSuccessObject.SUCCESS;
	}
	
	@PutMapping
	@ApiOperation(value = "学习资料更新", notes ="日志更新")
	public JsonObject<Object> updateMaterials(Materials obj) {
		service.updateMaterials(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询学习资料", notes ="学习资料详情")
	public JsonSuccessObject<Materials> getMaterials(@ApiParam(value="materialsID",required=false) @PathVariable("id") String id){
		Materials obj = service.getMaterials(id);
		JsonSuccessObject<Materials> returnResult = new JsonSuccessObject<>(obj);
		return returnResult;
	}
	
	@GetMapping("/")
	@ApiOperation(value = "查询多条学习资料", notes ="学习资料列表")
	@ApiImplicitParams({
	@ApiImplicitParam(name = "searchPublishState", value = "发布状态查询条件", paramType = "query"),
	@ApiImplicitParam(name = "searchCreateDateBegin", value = "学习资料查询开始时间", paramType = "query"),
	@ApiImplicitParam(name = "searchCreateDateEnd", value = "学习资料查询结束时间", paramType = "query"),
	@ApiImplicitParam(name = "searchMaterialsTitle", value = "学习资料名称查询", paramType = "query")})
	public JsonQueryObject<Materials> listMaterials(@ApiIgnore MaterialsQuery query,HttpServletRequest request){
		List<Materials> list = service.listMaterials(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/publishMaterials")
	@ApiOperation(value = "发布/撤销学习资料", notes ="")
	public JsonObject<Object> publishMaterials(@ApiParam(value = "materialsID", required = true) @RequestParam("materialsID") String materialsID,HttpServletRequest request){
		Materials obj = service.getMaterials(materialsID);
		Integer publishState=obj.getPublishState();
		if(null!=publishState && publishState==1) {
			obj.setPublishState(2);//撤销发布
		}else {
			obj.setPublishState(1);//发布
			obj.setPublishDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		}
		service.updateMaterials(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
}